home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / so91.lha / FileName / Part.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-23  |  7.1 KB  |  253 lines

  1. ;/* Part.c - AmigaMail File/Path separator example.  Compiled with SAS/C 5.10a.
  2. lc -cfis -v -d0 -b0 -j73 Part.c
  3. blink from Part.o to Part lib lib:amiga.lib ; if you don't have pragmas
  4. quit
  5.  */
  6. /* (c)  Copyright 1991 Commodore-Amiga, Inc.   All rights reserved.
  7. The information contained herein is subject to change without notice,
  8. and is provided "as is" without warranty of any kind, either expressed
  9. or implied.  The entire risk as to the use of this information is
  10. assumed by the user.
  11. */
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <dos/dos.h>
  15. #include <dos/dosextens.h>
  16. #include <dos/rdargs.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20.  
  21. /* def PRAGMAS if you have them */
  22. /* #define PRAGMAS */
  23. #ifdef PRAGMAS
  24. #include <pragmas/exec_pragmas.h>
  25. #include <pragmas/dos_pragmas.h>
  26. #else
  27. struct ExecBase *SysBase;
  28. struct Library *DOSBase;
  29.  
  30. #endif
  31.  
  32. VOID            main(VOID);
  33. LONG            GetPath(UBYTE * path, UBYTE * buffer, LONG buffersize);
  34. UBYTE          *ItsWild(UBYTE * string);
  35.  
  36. VOID main(VOID)
  37. {
  38. #ifdef PRAGMAS
  39.     struct Library *DOSBase;
  40.  
  41. #endif
  42.     struct RDArgs  *readargs;
  43.     LONG            rargs[2];
  44.     LONG            vargs[8];
  45.     UBYTE          *path, *filename;
  46.     UBYTE          *buffer;
  47.     UBYTE          *filepart, *pathpart;
  48.     struct Process *process;
  49.     BPTR            lock;
  50.     APTR            wptr;
  51.     BOOL            error;
  52.  
  53. #ifndef PRAGMAS
  54.     /* set up SysBase */
  55.     SysBase = (*((struct Library **) 4));
  56. #endif
  57.  
  58.     /* Fail silently if < 37 */
  59.     if (DOSBase = OpenLibrary("dos.library", 37))
  60.     {
  61.  
  62.         /*
  63.          * Use a generous 256 byte buffer. Should suffice for everything but
  64.          * extreme cases.
  65.          */
  66.         if (buffer = AllocMem(256, MEMF_CLEAR))
  67.         {
  68.  
  69.             if (readargs = ReadArgs("PATH/A,FILENAME/A", rargs, NULL))
  70.             {
  71.                 path = (UBYTE *) (rargs[0]);
  72.                 filename = (UBYTE *) (rargs[1]);
  73.  
  74.                 error = GetPath(path, buffer, 255);
  75.                 if (error)
  76.                     PrintFault(error, NULL);
  77.  
  78.                 filepart = FilePart(path);
  79.                 pathpart = PathPart(path);
  80.  
  81.                 vargs[0] = (LONG) path;
  82.                 vargs[1] = (LONG) filepart;
  83.                 vargs[2] = (LONG) pathpart;
  84.                 vargs[3] = (LONG) buffer;
  85.                 VFPrintf(Output(),
  86.                          "Filename: %s\nFilepart: %s\nPathpart: %s\nPath: %s\n",
  87.                          vargs);
  88.  
  89.                 /* No requesters */
  90.                 process = (struct Process *) FindTask(NULL);
  91.                 wptr = process->pr_WindowPtr;
  92.                 process->pr_WindowPtr = (APTR) - 1L;
  93.  
  94.                 /*
  95.                  * Make sure this name is for real. This will weed out names
  96.                  * like "dh0:/" and non-existent directories. (and also
  97.                  * complain about non-mounted volumes.) It is tempting to look
  98.                  * for trailing slashes and remove them but you shouldn't. You
  99.                  * might misinterpret the users intention. Better to generate a
  100.                  * warning and prompt for new input.
  101.                  */
  102.                 if (lock = Lock(buffer, SHARED_LOCK))
  103.                     UnLock(lock);
  104.                 else
  105.                     PrintFault(IoErr(), buffer);
  106.  
  107.                 /* Reset windowpointer */
  108.                 process->pr_WindowPtr = wptr;
  109.  
  110.                 /*
  111.                  * Normally we should respect the test for an invalid path. To
  112.                  * show the results however, we blunder along...
  113.                  *
  114.                  * Add the filename to the path.
  115.                  */
  116.                 if (AddPart(buffer, filename, 255))
  117.                     vargs[0] = (LONG) buffer;
  118.                 else
  119.                     vargs[0] = (LONG) "OVERFLOW";
  120.  
  121.                 VFPrintf(Output(), "\nNew path: %s\n", vargs);
  122.  
  123.                 FreeArgs(readargs);
  124.             }
  125.             else
  126.                 PrintFault(IoErr(), NULL);
  127.             FreeMem(buffer, 256);
  128.         }
  129.         CloseLibrary(DOSBase);
  130.     }
  131. }
  132.  
  133. /*
  134.  * Standalone function to isolate a path and copy it into a supplied buffer.
  135.  * Does not test if the path is valid. Returns an error in case of buffer
  136.  * overflow.
  137.  */
  138. LONG
  139. GetPath(UBYTE * path, UBYTE * buffer, LONG buffersize)
  140. {
  141.     UBYTE          *pathpart, *filepart;
  142.     UBYTE          *tmp1, *tmp2;
  143.     BPTR            lock;
  144.     struct FileInfoBlock *fib;
  145.     LONG            error = 0;
  146.  
  147.     /* Open own copy of dos.library if pragmas are used so it's standalone */
  148. #ifdef PRAGMAS
  149.     struct Library *DOSBase;
  150.  
  151.     if (!(DOSBase = OpenLibrary("dos.library", 36)))
  152.         return (1);
  153. #endif
  154.  
  155.     /*
  156.      * If there seems to be no path, the pathpart will point to the filepart
  157.      * too, so we need to check for that.
  158.      */
  159.     filepart = FilePart(path);
  160.     pathpart = PathPart(path);
  161.  
  162.     /*
  163.      * This also handles cases where there is only a volume/device name, only a
  164.      * directory name or a combo of those.
  165.      */
  166.     if (pathpart == path)
  167.     {
  168.  
  169.         /*
  170.          * There seems to be only one component. Copy it if it is not wild.
  171.          * Caller will have to check whether if it exists and if it is a file
  172.          * or directory.
  173.          */
  174.         if (!(ItsWild(pathpart)))
  175.             pathpart = NULL;
  176.     }
  177.  
  178.     if (pathpart != path)
  179.     {
  180.  
  181.         /*
  182.          * If pathpart equals filepart (pointer wise) then there is only one
  183.          * component (possible preceeded by a volume name).
  184.          */
  185.         if (pathpart == filepart)
  186.         {
  187.             if (!(ItsWild(pathpart)))
  188.                 pathpart = NULL;
  189.         }
  190.         else
  191.         {
  192.  
  193.             /*
  194.              * Try to lock it to determine if the last component is a
  195.              * directory.
  196.              */
  197.             if (lock = Lock(path, SHARED_LOCK))
  198.             {
  199.                 if (fib = AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR))
  200.                 {
  201.                     if ((Examine(lock, fib)) == DOSTRUE)
  202.                     {
  203.                         /* Hey it's a directory after all */
  204.                         if (fib->fib_DirEntryType > 0)
  205.                             pathpart = NULL;
  206.                     }
  207.                     FreeMem(fib, sizeof(struct FileInfoBlock));
  208.                 }
  209.                 UnLock(lock);
  210.             }           /* else treat it as a filename */
  211.         }
  212.  
  213.         /* Copy the pathpart in the buffer */
  214.         tmp1 = buffer;
  215.         tmp2 = path;
  216.         while ((*tmp1++ = *tmp2++) && (tmp2 != pathpart))
  217.         {
  218.             if (tmp1 == (buffer + buffersize))
  219.             {
  220.                 error = ERROR_NO_FREE_STORE;
  221.                 break;
  222.             }
  223.         }
  224.         *tmp1 = '\0';  /* NULL terminate. */
  225.     }
  226.  
  227. #ifdef PRAGMAS
  228.     CloseLibrary(DOSBase);
  229. #endif
  230.     return (error);
  231. }
  232.  
  233. /* Simple test whether a filename contains wildcards or not */
  234. UBYTE          *
  235. ItsWild(UBYTE * string)
  236. {
  237.     static UBYTE   *special = "#?*%([|";
  238.     UBYTE          *tmp = string;
  239.     COUNT           i;
  240.  
  241.     do
  242.     {
  243.         for (i = 0; special[i] != '\0'; i++)
  244.         {
  245.             if (*tmp == special[i])
  246.                 return (tmp);
  247.         }
  248.         tmp++;
  249.     } while (*tmp);
  250.  
  251.     return (NULL);
  252. }
  253.